const http       = require("http");
const path       = require("path");
const url        = require("url");
const fs         = require("fs");
  
const superagent = require("superagent");
const cheerio    = require("cheerio");

superagent
    .get("https://www.meizu.com/")
    .end((error,response)=>{
        //获取页面文档数据
        var content = response.text;
        //cheerio也就是nodejs下的jQuery  将整个文档包装成一个集合，定义一个变量$接收
        var $ = cheerio.load(content);
        //定义一个空数组，用来接收数据
        var result=[];
        //分析文档结构  先获取每个li 再遍历里面的内容(此时每个li里面就存放着我们想要获取的数据)
        $(".index-container .section-box").each((index,value)=>{

            let sectionTitle = $(value).children(".section-title").text();
            let sectionHref = $(value).children(".section-box-adv").find("a").attr("href");
            let sectionImg = $(value).children(".section-box-adv").find("img").attr("src");
            var childArr = [];

            $(value).children(".section-box-row").children(".index-center-wrapper").find("li").each((index1, value1) => {
                let aHref = $(value1).find("a").attr("href");
                let imgAddress = $(value1).find("a").children("img").attr("data-src");
                let goodsName = $(value1).find("a").find(".goods-name").text();
                let goodsDesc = $(value1).find("a").find(".goods-desc").text();
                let goodsPrice = $(value1).find("a").find(".goods-price").contents().not("s").text();
                let goodsDelPrice = $(value1).find("a").find(".goods-price").find("s").text();
                let productSign = $(value1).find("a").find(".product-sign").text();

                childArr.push({
                    aHref:aHref,
                    imgAddress:imgAddress,
                    goodsName:goodsName,
                    goodsDesc:goodsDesc,
                    goodsPrice:goodsPrice,
                    goodsDelPrice:goodsDelPrice,
                    productSign:productSign
                })
            })

            result.push({
                sectionTitle:sectionTitle,
                sectionHref:sectionHref,
                sectionImg:sectionImg,
                childArr:childArr
            });
            // console.log(typeof $(value).find(".info-primary").children().eq(1).html());
        });
        //将数组转换成字符串
        result=JSON.stringify(result);
        //将数组输出到json文件里  刷新目录 即可看到当前文件夹多出一个boss.json文件(打开boss.json文件，ctrl+A全选之后 ctrl+K，再Ctrl+F即可将json文件自动排版)
        fs.writeFile("catchMeizuCont.json",result,"utf-8",(error)=>{
            //监听错误，如正常输出，则打印null
            if(error==null){
                console.log("恭喜您，数据爬取成功!请打开json文件，先Ctrl+A，再Ctrl+K,最后Ctrl+F格式化后查看json文件(仅限Visual Studio Code编辑器)");
            }
        });
    });　　